package com.winit.openapi.erp.controller;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.winit.erp.spi.erptrack.AllocationErpTrackService;
import com.winit.erp.spi.erptrack.ExWarehouseErpTrackService;
import com.winit.erp.spi.erptrack.OrderErpTrackService;
import com.winit.erp.spi.erptrack.command.QueryAllocationTrackCommand;
import com.winit.erp.spi.erptrack.command.QueryExWarehouseTrackCommand;
import com.winit.erp.spi.erptrack.command.QueryOrderTrackCommand;
import com.winit.erp.spi.erptrack.vo.AllocationErpTrack;
import com.winit.erp.spi.erptrack.vo.ExWarehouseErpTrack;
import com.winit.erp.spi.erptrack.vo.OrderErpTrack;
import com.winit.openapi.constants.ErrorCode;
import com.winit.openapi.controller.BaseController;
import com.winit.openapi.exception.ApiException;
import com.winit.openapi.model.RequestMsg;
import com.winit.openapi.model.ResponseMsg;

/**
 * 
 * @author zhenguo.yin
 *
 */
@Controller
@RequestMapping(value="/erptrack")
public class ErpTrackController extends BaseController {
	@Resource
	private OrderErpTrackService orderErpTrackService;
	
	@Resource
	private AllocationErpTrackService allocationErpTrackService;
	
	
	@Resource
	private ExWarehouseErpTrackService exWarehouseErpTrackService;
	
    @RequestMapping(value="/queryOrderTrack")
    public String queryAllocationErpTrack(@ModelAttribute RequestMsg requestMsg,@ModelAttribute ResponseMsg responseMsg){
        Object obj = requestMsg.getData();
        if (obj instanceof JSONObject) {
            JSONObject json = (JSONObject) obj;
            QueryOrderTrackCommand queryOrderTrackTrackCommand = new QueryOrderTrackCommand();
            String orderNoStr = json.getString("documentNoList");
            List<String> orderNoList = JSONArray.parseArray(orderNoStr,String.class);
            queryOrderTrackTrackCommand.setDocumentNoList(orderNoList);
            queryOrderTrackTrackCommand.setLanguage(requestMsg.getLanguage());
//			List<OrderErpTrack> list= orderErpTrackService.getOrderErpTrackList(queryOrderTrackTrackCommand);
//            responseMsg.setData(list);
			Map<String,List<OrderErpTrack>> map= orderErpTrackService.getOrderErpTrackMap(queryOrderTrackTrackCommand);
            responseMsg.setData(map);
        } else {
            throw new ApiException(ErrorCode.FIELD_TYPE_ERROR, "data");
        }
        return SUCCESS;
    }
    
    @RequestMapping(value="/queryAllocationTrack")
    public String queryAllocationrErpTrack(@ModelAttribute RequestMsg requestMsg,@ModelAttribute ResponseMsg responseMsg){
        Object obj = requestMsg.getData();
        if (obj instanceof JSONObject) {
            JSONObject json = (JSONObject) obj;
            QueryAllocationTrackCommand queryAllocationTrackCommand = new QueryAllocationTrackCommand();
            String orderNoStr = json.getString("documentNoList");
            List<String> orderNoList = JSONArray.parseArray(orderNoStr,String.class);
			queryAllocationTrackCommand.setDocumentNoList(orderNoList);
			queryAllocationTrackCommand.setLanguage(requestMsg.getLanguage());
			Map<String,List<AllocationErpTrack>> map= allocationErpTrackService.getAllocationErpTrackMap(queryAllocationTrackCommand);
            responseMsg.setData(map);
        } else {
            throw new ApiException(ErrorCode.FIELD_TYPE_ERROR, "data");
        }
        return SUCCESS;
    }
    
    @RequestMapping(value="/queryExWarehouseTrack")
    public String queryExWarehouseErpTrack(@ModelAttribute RequestMsg requestMsg,@ModelAttribute ResponseMsg responseMsg){
        Object obj = requestMsg.getData();
        if (obj instanceof JSONObject) {
            JSONObject json = (JSONObject) obj;
            QueryExWarehouseTrackCommand queryExWarehouseTrackCommand = new QueryExWarehouseTrackCommand();
            String orderNoStr = json.getString("documentNoList");
            List<String> orderNoList = JSONArray.parseArray(orderNoStr,String.class);
            queryExWarehouseTrackCommand.setDocumentNoList(orderNoList);
            queryExWarehouseTrackCommand.setLanguage(requestMsg.getLanguage());
			Map<String,List<ExWarehouseErpTrack>> map= exWarehouseErpTrackService.getExWarehouseErpTrackMap(queryExWarehouseTrackCommand);
            responseMsg.setData(map);
        } else {
            throw new ApiException(ErrorCode.FIELD_TYPE_ERROR, "data");
        }
        return SUCCESS;
    }
}
